package cn.itcast.com.relection.tesxt;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by 郭浩 on 2020/9/10 14:28
 */
public class ref {
    public static void main(String[] args) {
        try {
            //通过反射获得类
            student student1 = new student();
            Class<student> student = (Class<cn.itcast.com.relection.tesxt.student>) student1.getClass();
            cn.itcast.com.relection.tesxt.student p1 = student.newInstance();
//            Field username = student.getDeclaredField("username");
//            username.setAccessible(true);
//            username.set(p1,"lllll");
            //获得setUsername方法
            Method setUsername = student.getDeclaredMethod("setUsername",String.class);
            setUsername.setAccessible(true);
            setUsername.invoke(p1,"jack");

            //获得getUsername方法
            Method getUsername = student.getDeclaredMethod("getUsername");
            getUsername.setAccessible(true);
            Object invoke = getUsername.invoke(p1);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
